package com.stylefeng.guns.modular.expenditure.controller;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.aop.Platform;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.expenditure.service.IExpenditureService;
import com.stylefeng.guns.modular.system.model.Expenditure;
import com.stylefeng.guns.modular.system.service.IDeptService;
import com.stylefeng.guns.modular.system.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 银行收支日结报表控制器
 *
 * @author fengshuonan
 * @Date 2018-07-02 14:43:07
 */
@Controller
@RequestMapping("/expenditure")
public class ExpenditureController extends BaseController {

    private String PREFIX = "/expenditure/expenditure/";

    @Autowired
    private IExpenditureService expenditureService;


    /**
     * 跳转到银行收支日结报表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "expenditure.html";
    }

    /**
     * 跳转到添加银行收支日结报表
     */
    @RequestMapping("/expenditure_add")
    public String expenditureAdd() {
        return PREFIX + "expenditure_add.html";
    }

    /**
     * 跳转到修改银行收支日结报表
     */
    @RequestMapping("/expenditure_update/{expenditureId}")
    public String expenditureUpdate(@PathVariable Integer expenditureId, Model model) {
        Expenditure expenditure =  expenditureService.selectById(expenditureId);
        model.addAttribute("item",expenditure);
        LogObjectHolder.me().set(expenditure);
        return PREFIX + "expenditure_edit.html";
    }

    /**
     * 获取银行收支日结报表列表
     * @author laoyu
     * @Date 2018-07-24
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@Platform String platform, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(beginTime)){
            beginTime=simpleDateFormat.format(new Date());
        }
        if (StringUtils.isBlank(endTime)){
            endTime = simpleDateFormat.format(DateUtil.getBeforeDay(new Date()));
        }
        //分页查询
        Page<Expenditure> page = new PageFactory<Expenditure>().defaultPage();


        return  super.packForBT(expenditureService.getBankday(beginTime,endTime,platform,page));

    }

    /**
     * 新增银行收支日结报表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增银行收支日结报表")
    public Object add(Expenditure expenditure) {
        if (expenditure.getRevdate()==null){
            throw new GunsException(BizExceptionEnum.BUSINSS_ERROR,"报表日期不能为空");
        }
        expenditure.setPlatform(ShiroKit.getUser().getDeptcode());
        expenditure.setCreattime(new Date());
        //组装查询
        EntityWrapper<Expenditure> wrapper = new EntityWrapper<Expenditure>();
        wrapper.eq("platform",ShiroKit.getUser().getDeptcode());
        wrapper.eq("revdate",expenditure.getRevdate());
        if(expenditureService.selectCount(wrapper)>0){
            throw new GunsException(BizExceptionEnum.BUSINSS_ERROR,DateUtil.formatDate(expenditure.getRevdate(),null)+"报表数据已存在");
        }
        expenditureService.insert(expenditure);
        return SUCCESS_TIP;
    }

    /**
     * 删除银行收支日结报表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除银行收支日结报表")
    public Object delete(@RequestParam Integer expenditureId) {
        expenditureService.deleteById(expenditureId);
        return SUCCESS_TIP;
    }

    /**
     * 修改银行收支日结报表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改银行收支日结报表")
    public Object update(Expenditure expenditure) {
        expenditure.setPlatform(ShiroKit.getUser().getDeptcode());
        expenditure.setUpdatetime(new Date());
        expenditureService.updateById(expenditure);
        return SUCCESS_TIP;
    }

    /**
     * 银行收支日结报表详情
     */
    @RequestMapping(value = "/detail/{expenditureId}")
    @ResponseBody
    public Object detail(@PathVariable("expenditureId") Integer expenditureId) {
        return expenditureService.selectById(expenditureId);
    }

    /**
     * 跳转到导出页面
     */
    @RequestMapping("/expenditure_export")
    public String expenditureExport() {
        return PREFIX + "expenditure_export.html";
    }
    /**
     * 描述:导出银行收支日结
     * 创建人: xiang
     * 日期: 2018/8/2
     * 时间: 9:58
     */
    @RequestMapping("/exportExpenditure")
    public void exportExpenditure(ModelMap modelMap, HttpServletRequest request,
                           HttpServletResponse response, @RequestParam(required = true) String beginTime, @RequestParam(required = true) String endTime) {

        Map<String, Object> map = new HashMap<String, Object>();
        TemplateExportParams params = new TemplateExportParams(
                "exceldoc/expenditure.xlsx");

        map.put("gh", ShiroKit.getUser().getDeptName());
        map.put("beginDate", beginTime);
        map.put("endDate", endTime);
        //传入平台 报表时间
        expenditureService.exportExpenditure(map);
        modelMap.put(TemplateExcelConstants.FILE_NAME, beginTime+"至"+endTime+ShiroKit.getUser().getDeptName()+"银行第三方收支日报总表");
        modelMap.put(TemplateExcelConstants.PARAMS, params);
        modelMap.put(TemplateExcelConstants.MAP_DATA, map);
        PoiBaseView.render(modelMap, request, response,
                TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);

    }


    /**
     * 获取银行收支日结报表 合计
     * @author laoyu
     * @Date 2018-07-24
     */
    @RequestMapping(value = "/tatol")
    @ResponseBody
    public Object tatol(@Platform String platform,@RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isBlank(beginTime)){
            beginTime=simpleDateFormat.format(new Date());
        }
        if (StringUtils.isBlank(endTime)){
            endTime = simpleDateFormat.format(DateUtil.getBeforeDay(new Date()));
        }
        //组装查询
        EntityWrapper<Expenditure> wrapper = new EntityWrapper<Expenditure>();
        wrapper.ge("revdate", beginTime);
        wrapper.le("revdate", endTime);
        wrapper.eq("platform",platform);

        return  expenditureService.total(wrapper);

    }



}
